#include "themewindow.h"
#include "ui_themewindow.h"

ThemeWindow::ThemeWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ThemeWindow)
{
    ui->setupUi(this);
    // 不继承父类样式
    setAttribute(Qt::WA_StyledBackground);
    loadThemes();
}

ThemeWindow::~ThemeWindow()
{
    delete ui;
}

void ThemeWindow::paintEvent(QPaintEvent *event)
{
    // 移动到指定位置
    QWidget *p = qobject_cast<QWidget*>(parent());
    move(p->width() - 140 - width(), 80);
}

void ThemeWindow::loadThemes()
{
    // 清空现有缓存
    for(auto item : themeItems){
        this->ui->m_items->layout()->removeWidget(item);
        item->deleteLater();
    }
    themeItems.clear();

    // 重新加载
    ThemeManager &themeManager = ThemeManager::getInstance();
    themeManager.loadTheme();
    const QVector<Theme> themes = themeManager.getThemes();
    for(auto theme : themes){
        ThemeItem *themeItem = new ThemeItem();
        themeItem->setTheme(theme);
        this->ui->m_items->layout()->addWidget(themeItem);
        connect(themeItem, &ThemeItem::setCurrentTheme, [=](){
           emit changeTheme(themeItem->getTheme());
        });
    }
}
